package com.smartwebx.ha.web.admin.module.screen.area;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.dataresolver.Param;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaGroupManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaGroup;
import com.smartwebx.ha.dal.bean.AreaAndGroup;

public class AjaxGetArea {
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HaGroupManager haGroupManager;
	
	public void execute(@Param("id") Integer areaId) throws IOException{
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		ObjectMapper mapper = new ObjectMapper();
		HaArea area=haAreaManager.findOne(areaId);
		HaGroup group=haGroupManager.findOne(area.getGroupId());
		AreaAndGroup areaAndGroup=new AreaAndGroup();
		BeanUtils.copyProperties(area,areaAndGroup);
		String[] str=area.getCenterPoint().split(",");
		String longitude=str[0];
		String latitude=str[1];
		areaAndGroup.setLongitude(Double.parseDouble(longitude));
		areaAndGroup.setLatitude(Double.parseDouble(latitude));
		areaAndGroup.setGroupName(group.getName());
		
		mapper.writeValue(out, areaAndGroup);
	}

}
